package nl.rug.ml.dobbelen.learning;

import java.util.LinkedList;
import java.util.List;

import nl.rug.ml.dobbelen.game.Dice;

/**
 * @author Do Bich Ngoc (bichngocdo89@gmail.com)
 *
 */
public class DobbelenUtils {

	public static int[] createBlankHistogram() {
		return new int[6];
	}

	public static int[] dicesToHistogram(List<Dice> dices) {
		int[] histogram = DobbelenUtils.createBlankHistogram();
		for (Dice dice : dices) {
			histogram[dice.getValue() - 1]++;
		}
		return histogram;
	}

	public static List<Dice> histogramToHeldDice(int[] histogram, List<Dice> dices) {
		List<Dice> heldDices = new LinkedList<Dice>();
		for (Dice dice : dices) {
			if (histogram[dice.getValue()] > 0) {
				heldDices.add(dice);
				histogram[dice.getValue()]--;
			}
		}
		for (int i = 0; i < histogram.length; i++) {
			if (histogram[i] != 0) {
				System.err.println("Histogram and dices are incompatible");
			}
		}
		return heldDices;
	}

}
